﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using System.Speech.Synthesis;
using System.Collections.ObjectModel;
using System.Windows.Forms;
using Office = Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;
using TextToSpeach.Engine;
using TextToSpeach.Engine.Exceptions;

namespace TTS_G0
{
    public partial class TTSRibbon : OfficeRibbon
    {
        private SapiEngine sapiEngine = new SapiEngine();


        public TTSRibbon()
        {
            InitializeComponent();
            for (int i = -10; i < 11; i++)
            {
                RibbonButton rddi = new RibbonButton();
                rddi.Label = i.ToString() ;
                menuSpeed.Items.Add(rddi);
                rddi.Click += new EventHandler<RibbonControlEventArgs>(rddi_Click);
            }
            for (int i = 0; i < 101; i++)
            {
                RibbonButton rddi2 = new RibbonButton();
                rddi2.Label = i.ToString();
                menuVolume.Items.Add(rddi2);
                rddi2.Click += new EventHandler<RibbonControlEventArgs>(rddi2_Click);
            }



            sapiEngine.Speed = -1;
            sapiEngine.Volume = 50;
        }

        void rddi2_Click(object sender, RibbonControlEventArgs e)
        {
            menuVolume.Label = "Volume: " + ((RibbonButton)sender).Label;
            sapiEngine.Volume = int.Parse(((RibbonButton)sender).Label);
        }

        void rddi_Click(object sender, RibbonControlEventArgs e)
        {
            menuSpeed.Label = "Speed: " + ((RibbonButton)sender).Label;
            sapiEngine.Speed = int.Parse(((RibbonButton)sender).Label);
        }

        private void TTSRibbon_Load(object sender, RibbonUIEventArgs e)
        {
            PopulateVoiceList();
            dropDownVoice.SelectedItemIndex = 0;
            dropDownVoice_SelectionChanged(null, null);
        }

        public void PopulateVoiceList()
        {
            ReadOnlyCollection<InstalledVoice> voices = sapiEngine.GetInstalledVoices();

            try
            {
                VoiceInfo voiceInfo;
                foreach (InstalledVoice v in voices)
                {
                    voiceInfo = v.VoiceInfo;
                    RibbonDropDownItem rddi = new RibbonDropDownItem();
                    rddi.Label = voiceInfo.Name.ToString();
                    dropDownVoice.Items.Add(rddi);
                }

            }
            catch (NoVoicesException e)
            {
                MessageBox.Show(e.Message, "Text-To-Speach", MessageBoxButtons.OK);
                speakButton.Enabled = false;
                stopButton.Enabled = false;
                dropDownVoice.Enabled = false;
            }
            catch (EmptyVoicesException e)
            {
                MessageBox.Show(e.Message, "Text-To-Speach", MessageBoxButtons.OK);
            }
            catch (Exception e)
            {
                MessageBox.Show("Unrecognized error: \n\t" + e.Message + "\nPlease use the \"Report Bug\" button to report waht happened.", "Text-To-Speach", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            }
        }

        private void buttonInfo_Click(object sender, RibbonControlEventArgs e)
        {
            AboutBox about = new AboutBox();
            about.ShowDialog();
        }

        private void speakButton_Click(object sender, RibbonControlEventArgs e)
        {  

            Word.Document document = Globals.ThisAddIn.Application.ActiveDocument;

            sapiEngine.StartSynthesis(dropDownVoice.SelectedItem.Label, document.Content.Text);
        }

        private void stopButton_Click(object sender, RibbonControlEventArgs e)
        {
            sapiEngine.StopSynthesis();
        }

        private void dropDownVoice_SelectionChanged(object sender, RibbonControlEventArgs e)
        {
            VoiceInfo voiceInfo = sapiEngine.GetInstalledVoices()[dropDownVoice.SelectedItemIndex].VoiceInfo;

            labelName.Label = "Name: " + voiceInfo.Name;
            labelAge.Label = "Age: " + voiceInfo.Age.ToString();
            labelGender.Label = "Gender: " + voiceInfo.Gender.ToString();
        }

        private void websiteButton_Click(object sender, RibbonControlEventArgs e)
        {
            System.Diagnostics.Process.Start("http://gravity0.site90.com/index.php/text-to-speach");
        }

        private void buttonBug_Click(object sender, RibbonControlEventArgs e)
        {
            System.Diagnostics.Process.Start("mailto:bugs@gravity0.site90.com?subject=Text-To-Speach Bug");
        }
    }
}
